﻿using System;
using System.Reflection; //反射
using System.Configuration; //配置管理
using Full = FactoryMethodPattern.Full; //工厂模式完整版本
using Simplified = FactoryMethodPattern.Simplified; //工厂模式简化版本


namespace FactoryMethodPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello FactoryMethodPattern/FactoryPattern!");

            /* 工厂模式完整版本-Full */
            {
                //文件日志
                Full.LoggerFactory factory = new Full.FileLoggerFactory();
                Full.Logger logger = factory.CreateLogger();
                logger.WriteLog();
            }

            {
                //数据库日志
                Full.LoggerFactory factory = new Full.DatabaseLoggerFactory();
                Full.Logger logger = factory.CreateLogger();
                logger.WriteLog();
            }

            {
                //xml配置文件与反射方式扩展
                // 1.读取【App.config】配置文件
                string factoryString = ConfigurationManager.AppSettings["factoryFull"];
                // 参数"FactoryMethodPattern"是工厂类所在的命名空间的名称，"FactoryMethodPattern.Full.FileLoggerFactory" 具体工厂类类名
                Full.LoggerFactory factory = (Full.LoggerFactory)Assembly.Load("FactoryMethodPattern").CreateInstance(factoryString);
                Full.Logger logger = factory.CreateLogger();
                logger.WriteLog();
            }

            /* 工厂模式简化版本-Simplified */
            {
                //xml配置文件与反射方式扩展
                // 1.读取【App.config】配置文件
                string factoryString = ConfigurationManager.AppSettings["factorySimplified"];
                // 参数"FactoryMethodPattern"是工厂类所在的命名空间的名称，"FactoryMethodPattern.Simplified.FileLoggerFactory" 具体工厂类类名
                Simplified.LoggerFactory factory = (Simplified.LoggerFactory)Assembly.Load("FactoryMethodPattern").CreateInstance(factoryString);
                factory.WriteLog();
            }
        }
    }
}
